import 'dart:async';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'dart:ui';
import 'main.dart';
import 'login.dart';

void main() => runApp(SplashPage());

class SplashPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "VIP",
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new SplashStatefulWidget());
  }
}

class SplashStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new SplashState();
  }
}

class SplashState extends State {
  Timer timer;
  int count = 3;
  final startTime = 1;

  @override
  void initState() {
    super.initState();
    timer = new Timer.periodic(const Duration(seconds: 1), (v) {
      if (count <= startTime) {
        timer.cancel();
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) {
          return new LoginPageStatelessWidget();
        }));
      } else {
        setState(() {
          count--;
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: SafeArea(
          child: Stack(
        alignment: AlignmentDirectional.topEnd,
        children: <Widget>[
          Container(
            child: (Wrap(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 130.0),
                  child: Image.asset(
                    'images/icon_splash.png',
                    width: 80,
                    height: 88,
                    color: Colors.blue,
                  ),
                ),
                Padding(
                    padding: EdgeInsets.only(top: 275.0),
                    child: Text(
                      "猫扑",
                      style: TextStyle(color: Color(0xff43465f), fontSize: 30),
                    )),
                Padding(
                    padding: EdgeInsets.only(top: 30.0),
                    child: Text(
                      "捕捉精彩，分享快乐",
                      style: TextStyle(color: Color(0xff006fff), fontSize: 18),
                    )),
                Offstage(
                  //true 隐藏 false 显示
                  offstage: true,
                  child: Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: FlatButton(
                        onPressed: () {
                          Navigator.of(context).pushReplacement(
                              MaterialPageRoute(builder: (context) {
                            return new MyApp();
                          }));
                        },
                        child: Text('跳转')),
                  ),
                ),
              ],
            )),
            alignment: Alignment.topCenter,
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.0, right: 20.0),
            child: Container(
              padding: EdgeInsets.all(5.0),
              decoration: BoxDecoration(
                  color: Color(0xffdddddd), shape: BoxShape.circle),
              child: Text(
                count.toString() + "s",
                style: TextStyle(color: Colors.grey),
              ),
            ),
          )
        ],
      )),
    );
  }
}
